import { useAuthStore } from "@/store/modules/auth.js";
import { stringifyQuery } from "@/utils/index.js";
import { LOGIN_PATH, router } from "../router";
import {
  getStorage,
  setStorage,
  removeStorage,
} from "@/utils/storage/index.js";
// export const BASE_URL = "http://10.10.100.52:38080";
// export const BASE_URL = "http://42.192.66.95/grotto";
// export const BASE_URL = "http://114.117.242.249:38088";
// export const BASE_URL = "http://192.168.100.13:38080";
// export const BASE_URL = "http://124.222.91.76:38080";
// export const BASE_FILE_URL = "http://124.222.91.76:38080/file/download?url=";

// test 作业票 admin admin111
//  export const BASE_FILE_URL = "http://82.156.4.210:8118/file/download?url=";
//  export const BASE_URL = "http://82.156.4.210:8118";

// test 一企一品
//  export const BASE_FILE_URL = "http://82.156.4.210:8112/file/download?url=";
//  export const BASE_URL = "http://82.156.4.210:8112";

// test 一企一品/作业票 通用
// export const BASE_FILE_URL = "http://82.156.4.210:8116/file/download?url=";
// export const BASE_URL = "http://82.156.4.210:8116";

// pro admin Sdkmqzq2023
export const BASE_FILE_URL = "http://42.192.66.95/grotto/file/download?url=";
export const BASE_URL = "http://42.192.66.95/grotto";

// export const BASE_FILE_URL = "https://sdkmqzq.shidongkougas.com.cn/file/download?url=";
// export const BASE_URL = "https://sdkmqzq.shidongkougas.com.cn";

export default function requset(options) {
  uni.showLoading({
    mask: true,
  });
  const authStore = useAuthStore();

  console.log('options', options.flag)
  options.url = BASE_URL + options.url;
  // options.url = options.flag == 'yqyp' ? `${BASE_URL_YQYP}${options.url}` : `${BASE_URL}${options.url}`

  if (options.params) {
    options.url += stringifyQuery(options.params);
  }
  options.method = options.method.toLocaleUpperCase();
  let token = authStore.getToken || authStore.token;
  if (!token) {
    token = getStorage("TOKEN") || null;
    authStore.setToken(token);
  }

  options.header = Object.assign(
    {},
    options.header,
    token && { authorization: `Bearer ${token}` }
  );

  return uni
    .request(options)
    .then((res) => {
      // 响应处理
      uni.hideLoading();

      if (res.statusCode === 404) {
        uni.showToast({
          title: "接口404",
        });
        return Promise.reject(res);
      }
      const { code, msg } = res.data;
      if (code === 401) {
        uni.showModal({
          title: "提示",
          content: "登陆失效，请重新登陆",
          success: function (res) {
            if (res.confirm) {
              authStore.setToken(null);
              router.reLaunch({ path: LOGIN_PATH });
            } else if (res.cancel) {
              console.log("用户点击取消");
            }
          },
        });
        return Promise.reject("无效的会话，或者会话已过期，请重新登录。");
      } else if (code === 500) {
        uni.showToast({
          icon: 'none',
          title: msg
        });

        return Promise.reject(new Error(msg));
      } else if (code === 601) {
        uni.showToast({
          title: msg,
        });
        return Promise.reject(new Error(msg));
      } else if (code && code !== 200) {
        uni.showToast({
          icon: 'none',
          title: msg
        });
        return Promise.reject("error");
      } else {
        return Promise.resolve(res.data);
      }
    })
    .catch((error) => {
      // 错误处理
      uni.hideLoading();
      console.log("error", error);
      return Promise.reject(error);
    })
    .finally(() => {
      uni.hideLoading();
    });
}
